/* Regression for the longer panel   */
clear all
set more off
cd ""    //target directory


*** Inputs from Matlab
clear
insheet using choice.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newchoice`i'
}
reshape long newchoice,i(ahaid year) j(vid)
tempfile chs
save `chs'


insheet using vlag.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newvlag`i'
}
reshape long newvlag,i(ahaid year) j(vid)
tempfile vlag
save `vlag'


insheet using exov1mkt.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newv1mkt`i'
}
reshape long newv1mkt,i(ahaid year) j(vid)
tempfile v1mkt
save `v1mkt'



insheet using exov2mkt.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newv2mkt`i'
}
reshape long newv2mkt,i(ahaid year) j(vid)
tempfile v2mkt
save `v2mkt'


insheet using exovmksh.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newvmktsh`i'
}
reshape long newvmktsh,i(ahaid year) j(vid)
tempfile vmktsh
save `vmktsh'


insheet using v1sys.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newv1sys`i'
}
reshape long newv1sys,i(ahaid year) j(vid)
tempfile v1sys
save `v1sys'


insheet using v2sys.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newv2sys`i'
}
reshape long newv2sys,i(ahaid year) j(vid)
tempfile v2sys
save `v2sys'


insheet using vsysh.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newvsysh`i'
}
reshape long newvsysh,i(ahaid year) j(vid)
tempfile vsysh
save `vsysh'


insheet using exovtipmsh.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newvtipmsh`i'
}
reshape long newvtipmsh,i(ahaid year) j(vid)
merge 1:1 ahaid year vid using `chs'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `vlag'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `v1mkt'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `v2mkt'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `vmktsh'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `v1sys'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `v2sys'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `vsysh'
keep if _merge==3
drop _merge
tempfile vtipmsh
save `vtipmsh'


*** IV inputs
clear
insheet using "./prepare/iv_dom.csv",clear
rename (v14 v15) (hsa year)
forvalues i=1/13 {
rename v`i' ivdom`i'
}
reshape long ivdom,i(hsa year) j(vid)
tempfile ivdom
save `ivdom'


insheet using "./prepare/iv_msh.csv",clear
rename (v14 v15) (hsa year)
forvalues i=1/13 {
rename v`i' ivmsh`i'
}
reshape long ivmsh,i(hsa year) j(vid)
tempfile ivmsh
save `ivmsh'



*** Prepare the MAIN file
clear
use "./prepare/allbasic_oldyrs.dta",clear
keep if year>1999
keep id year vnd
replace year=year-1
rename vnd adj_vnd
tempfile adjvnd
save `adjvnd'


clear
use "./prepare/allbasic.dta",clear
keep if year==2006
replace year=year-1
keep id year prechs
rename prechs adj_vnd
append using `adjvnd'
tempfile adj_vnd
save `adj_vnd'


clear
use "./prepare/allbasic_oldyrs",clear
merge 1:1 id year using `adj_vnd'
keep if _merge==3
drop _merge
rename vnd prechs
append using "./prepare/allbasic.dta",force


merge 1:m ahaid year using `vtipmsh'
keep if _merge==3
drop _merge


merge m:1 hsa year vid using `ivdom'
drop if _merge==2
drop _merge


merge m:1 hsa year vid using `ivmsh'
drop if _merge==2
drop _merge


reg newvmktsh ivdom ivmsh if prechs==1&sysid==0
predict uhat,residual

gen uhat2=uhat*uhat
gen uhat3=uhat*uhat*uhat



sum bdtot if sysid==0,detail
gen bdcat=0
replace bdcat=1 if bdtot<=r(p25)
replace bdcat=2 if bdtot<=r(p50)&bdtot>r(p25)
replace bdcat=3 if bdtot<=r(p75)&bdtot>r(p50)
replace bdcat=4 if bdtot>r(p75)

tab bdcat,gen(bdqntl)



foreach i in 1 2 3 4 {
gen v1bd`i'=newv1mkt*bdqntl`i'
gen vmshbd`i'=newvmktsh*bdqntl`i'
gen iv1bd`i'=ivdom*bdqntl`i'
gen ivmshbd`i'=ivmsh*bdqntl`i'
}



sort ahaid year vid
egen nunuid=group(ahaid year)


********* Now incorporate new inputs for market (un)observables
***%%%*** ### UPDATED on 04/09/2020: Demographics: population and income per capita
preserve
clear
use "./prepare/demographic4mkt_oldyrs"
duplicates drop hsa year,force
keep hsa year totpop totpop65 avginc
gen perc65=totpop65/totpop
replace totpop=log(totpop)
replace totpop65=log(totpop65)
replace avginc=log(avginc)
tempfile pop
save `pop'
restore
***%%%***



***%%%*** ### UPDATED on 04/09/2020: create HHI at the market level using total admissions: admtot (constructed internally using the file "for2ndstage")
preserve
keep if vid==13
keep id year admtot hsa
bys hsa year:egen totadm_mkt=total(admtot)
gen admsh=admtot/totadm_mkt
bys hsa year:egen hhi=total(admsh^2)
duplicates drop hsa year,force
keep hsa year hhi
tempfile hhi
save `hhi'
restore
***%%%***



***%%%*** ### UPDATED on 05/17/2020: Use the combined market FEs---mktFEs_comb1 (using adoption ratio as DV)
preserve

clear
input hsa mktfe
1 0
end
tempfile firsthsa
save `firsthsa'


clear
import delim using "./prepare/mktFEs_comb1.csv",delimiters(",") varnames(1)
rename (v1 comb) (hsa mktfe)
drop if strpos(hsa,"_Ihsa_")==0
replace hsa=subinstr(hsa,"_Ihsa_","",1)
destring,replace force
append using `firsthsa',force

gen below25cat=0
gen below50cat=0
gen below75cat=0
gen below100cat=0

sum mktfe,detail

replace below25cat=1 if mktfe<=r(p25)
replace below50cat=1 if mktfe<=r(p50)&mktfe>r(p25)
replace below75cat=1 if mktfe<=r(p75)&mktfe>r(p50)
replace below100cat=1 if mktfe>r(p75)

tempfile mktfe_comb
save `mktfe_comb'

restore
***%%%***




** MErge stuff
merge m:1 hsa using `mktfe_comb'
keep if _merge==3
drop _merge


merge m:1 hsa year using `pop'
keep if _merge==3
drop _merge

merge m:1 hsa year using `hhi'
keep if _merge==3
drop _merge




preserve
set more off
keep if sysid==0
keep if prechs==1

gen mktcat1=below25cat
gen mktcat2=below50cat
gen mktcat3=below75cat
gen mktcat4=below100cat

replace below25cat=0 if vid==1
replace below50cat=0 if vid==1
replace below75cat=0 if vid==1
replace below100cat=0 if vid==1



********** Report results in Dec 2020

forvalues i=2/13 {
gen bed`i'=bdtot*(vid==`i')
}



****** Non-linear
xi:clogit newchoice vmshbd1 vmshbd2 vmshbd3 vmshbd4 bed* below50cat below75cat below100cat i.vid*hhi i.vid*totpop65 if prechs==1&sysid==0,group(nunuid) vce(r)  
est sto nonivoyr

xi:clogit newchoice vmshbd1 vmshbd2 vmshbd3 vmshbd4 bed* below50cat below75cat below100cat i.vid*hhi i.vid*totpop65 if prechs==1&sysid==0&uhat~=.,group(nunuid) vce(r)  
est sto nonivoyr_iv

xi:clogit newchoice vmshbd1 vmshbd2 vmshbd3 vmshbd4 bed* uhat uhat2 below50cat below75cat below100cat i.vid*hhi i.vid*totpop65 if prechs==1&sysid==0,group(nunuid) vce(boot)  
est sto ivoyr


****** Linear
xi:ivreg2 newchoice vmshbd1 vmshbd2 vmshbd3 vmshbd4 i.vid bed* below50cat below75cat below100cat i.vid*hhi i.vid*totpop65 if sysid==0&prechs==1,robust 
est sto noiv2sls


*** LPM on IV sample
xi:ivreg2 newchoice i.vid vmshbd1 vmshbd2 vmshbd3 vmshbd4 bed* below50cat below75cat below100cat i.vid*hhi i.vid*totpop65 if sysid==0&prechs==1&ivmsh~=.,robust  
est sto noiv2sls_iv


*** 2SLS
bootstrap, reps(50):ivreg2 newchoice _Ivid_* bed* below50cat below75cat below100cat i.vid*hhi i.vid*totpop65 (vmshbd1 vmshbd2 vmshbd3 vmshbd4=iv1bd1 iv1bd2 iv1bd3 iv1bd4 ivmshbd1 ivmshbd2 ivmshbd3 ivmshbd4) if sysid==0&prechs==1,robust  
est sto iv

